#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <sys/types.h>
#include <sys/socket.h>

#include <netinet/in.h>
#include <netinet/ip.h> /* superset of previous */
#include <arpa/inet.h>  /* for sockaddr_in and inet_addr() */


int SocketConnect(char* _address, int _connectPort)
{
    int                 sockFd;
    struct sockaddr_in  addr  ;

    sockFd = socket(AF_INET,        /*  Specifies the communications domain in which a socket is to be created  */
                   SOCK_STREAM ,    /*  Specifies the type of socket to be created.   */
                   IPPROTO_TCP);    /*  Specifies a particular protocol to be used with the socket. Specifying a protocol of 0 causes socket() to use an unspecified default proto‐
                                        col appropriate for the requested socket type.    */
    if(sockFd == -1)
    {
        perror("socket");
        return -1;
    }

    /* Construct the server sockaddr_in structure */
    memset(&addr, 0, sizeof(addr));                 /* Clear struct */
    addr.sin_family = AF_INET;                      /* Internet/IP  */
    addr.sin_addr.s_addr =inet_addr("127.0.0.1");   /* IP address   */
    addr.sin_port = htons(_connectPort);                              /* server port */

    if (!inet_aton(_address, (struct in_addr *) &addr.sin_addr.s_addr))
    {
        perror("bad IP address format");
        close(sockFd);
        return -1;
    }

    if (connect(sockFd,                 /*  socket file descriptor                                  */
        (struct sockaddr *) &addr,      /*  assigns the address specified to the socket             */
        sizeof(addr)) < 0)              /*  specifies the size, in bytes, of the address structure  */
    {
        perror("connect");
        close(sockFd);
        return -1;
    }

    return sockFd;
}


int main()
{

    return 0;
}
